//
//  MyLookedViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "MyLookedViewController.h"
#import "DetailJiuDianViewController.h"

#import "HBHMyLookCell.h"
@interface MyLookedViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@end

@implementation MyLookedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    [self getBrowse];
}

-(void)creatView
{
    _dataArr = [NSMutableArray new];
    _pageNumber = 1;
    [self initTitleWithTitle:@"我的浏览记录"];
    [self initRightBarButtonWithTitle:@"清空" andTitleColor:[UIColor whiteColor] andFont:16*screenW_6];
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 160*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            _pageNumber = 1;
            [_dataArr removeAllObjects];
            [self getBrowse];
        }];
        //加载
        _tableV.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            _pageNumber++;
            [self getBrowse];
        }];
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HBHMyLookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHMyLookCell"];
    if (!cell) {
        cell = [[HBHMyLookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHMyLookCell"];
    }
    [cell loadCellWithData:_dataArr[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160*screenW_6;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self deleteCell:indexPath];//删除
}

-(void)deleteCell:(NSIndexPath *)index//删除事件
{
    [HudHelper  showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/deleteBrowseRecord" Dic:@{@"id":_dataArr[index.row][@"id"]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [_dataArr removeObjectAtIndex:index.row];
                    [_tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationTop];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count < indexPath.row + 1) {
        return;
    }
    DetailJiuDianViewController *vc = [DetailJiuDianViewController new];
    vc.hotelId = _dataArr[indexPath.row][@"businessId"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getBrowse
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/browseRecord" Dic:@{@"userId":[Storage getToken],@"pageNo":[NSString stringWithFormat:@"%ld",_pageNumber],@"pageSize":@"10"} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([responseDic[@"success"] boolValue]) {
                    if (_dataArr.count < [responseDic[@"count"] integerValue]) {
                        [_dataArr addObjectsFromArray:responseDic[@"data"]];
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [HudHelper hideHudToView:self.view];
                [_tableV reloadData];
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    if (_dataArr.count < [responseDic[@"count"] integerValue]) {
                        [_tableV.mj_footer endRefreshing];
                    }else{
                        [_tableV.mj_footer endRefreshingWithNoMoreData];
                    }
                }
            });
        }];
    });
}

-(void)onTapRightBarButton
{
    UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"确定清空记录" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnullaction) {
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction) {
        [HudHelper showProgressHudWithMessage:@"" toView:self.view];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [HttpRequest PostrequestUrl:@"/user/deleteUserBrowseRecord" Dic:@{@"jdUser.id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [HudHelper hideHudToView:self.view];
                    if ([responseDic[@"success"] boolValue]) {
                        [_dataArr removeAllObjects];
                        [_tableV reloadData];
                    }else{
                        [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                    }
                });
            }];
        });
    }]];
    [self presentViewController:alertController animated:YES completion:^{
    }];
}

@end
